#include <47func.h>
int main(int argc, char *argv[])
{
    // ./5_aqiang 192.168.118.128 1234
    ARGS_CHECK(argc,3);
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    // AF_INET ipv4 SOCK_STREAM TCP
    struct sockaddr_in serverAddr;//带in 是ipv4地址 带in6是ipv6地址 不带是通用地址
    serverAddr.sin_family = AF_INET;//ipv4
    serverAddr.sin_addr.s_addr = inet_addr(argv[1]);//把点分十进制的ip地址转换成32位大端存储的地址
    serverAddr.sin_port = htons(atoi(argv[2]));//把端口用大端存储
    int ret = connect(sockfd,(struct sockaddr *)&serverAddr,sizeof(serverAddr));
    ERROR_CHECK(ret,-1,"connect");
    fd_set rdset;
    char buf[4096];
    while(1){
        FD_ZERO(&rdset);
        FD_SET(STDIN_FILENO,&rdset);
        FD_SET(sockfd,&rdset);//sockfd相当于是之前管道的fdr
        select(sockfd+1,&rdset,NULL,NULL,NULL);
        if(FD_ISSET(STDIN_FILENO,&rdset)){
            bzero(buf,sizeof(buf));
            ssize_t sret = read(STDIN_FILENO,buf,sizeof(buf));
            if(sret == 0){
                send(sockfd,"nishigehaoren",13,0);
                break;
            }
            send(sockfd,buf,strlen(buf),0);
        }
        if(FD_ISSET(sockfd,&rdset)){
            bzero(buf,sizeof(buf));
            ssize_t sret = recv(sockfd,buf,sizeof(buf),0);
            if(sret == 0){
                printf("I quit!\n");
                break;
            }
            printf("buf = %s\n", buf);
        }
    }
    close(sockfd);
    return 0;
}

